package com.bjpowernode.controller;

import com.bjpowernode.entity.Task;
import com.bjpowernode.service.TaskService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 处理财务计划相关的请求
 *
 * @author 刘逸晖
 */
@RequestMapping(value = "/myWeb/task")
public class TaskController {

    /**
     * 处理财务计划相关的业务
     */
    private TaskService taskService;

    /**
     * 新增财务计划
     *
     * @param task 新增数据
     * @return 模型和试图
     */
    @RequestMapping(value = "/insert.do")
    public ModelAndView insert(Task task) {
        this.taskService.insertTask(task);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("info", "收支计划添加成功")
                .setViewName("info");

        return modelAndView;
    }

    /**
     * 查询所有财务计划
     *
     * @return 模型和试图
     */
    @RequestMapping(value = "/find.do")
    public ModelAndView findAll() {
        List<Task> tasks = this.taskService.selectAllTasks();

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("taskList", tasks)
                .setViewName("task");

        return modelAndView;
    }

    /**
     * 根据 id 删除财务计划
     *
     * @param id 财务计划的 id
     * @return 财务计划查询页
     */
    @RequestMapping(value = "/delete.do")
    public String delete(Integer id) {
        this.taskService.deleteTaskById(id);

        return "redirect:/myWeb/task/find.do";
    }

    /**
     * 预更新财务计划
     *
     * @param id 预更新财务计划的 id
     * @return 模型和试图
     */
    @RequestMapping(value = "/findById.do")
    public ModelAndView findById(int id) {
        Task task = this.taskService.selectTaskById(id);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("task", task)
                .setViewName("task_info");

        return modelAndView;
    }

    @RequestMapping(value = "/update.do")
    public ModelAndView update(Task task) {
        this.taskService.updateTaskById(task);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("info", "收支计划更新成功")
                .setViewName("info");

        return modelAndView;
    }

    public TaskService getTaskService() {
        return taskService;
    }

    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }
}
